package com.hz.ruoyifarmbase.farm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hz.ruoyifarmbase.basedata.service.IAgrGermBreedService;
import com.hz.ruoyifarmbase.baseinfo.domain.AgrBasePlots;
import com.hz.ruoyifarmbase.baseinfo.service.IAgrBasePlotsService;
import com.hz.ruoyifarmbase.farm.domain.AgrBaseBatch;
import com.hz.ruoyifarmbase.farm.domain.AgrBaseOperate;
import com.hz.ruoyifarmbase.farm.domain.AgrBaseProcess;
import com.hz.ruoyifarmbase.farm.domain.vo.AgrBatchBreedVo;
import com.hz.ruoyifarmbase.farm.mapper.AgrBaseBatchMapper;
import com.hz.ruoyifarmbase.farm.service.AgrBaseBatchService;
import com.hz.ruoyifarmbase.farm.service.AgrBaseProcessService;
import com.hz.ruoyifarmbase.farm.service.IAgrBaseOperateService;
import com.hz.ruoyifarmbase.farm.utils.HarvestDateCalculator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author 侯文波
 * @date 2025/3/7 10:18
 * @description:
 */
@Service
public class AgrBaseBatchServiceImpl extends ServiceImpl<AgrBaseBatchMapper, AgrBaseBatch> implements AgrBaseBatchService {

    @Autowired
    private AgrBaseBatchMapper agrBaseBatchMapper;
    @Autowired
    private IAgrGermBreedService agrGermBreedService;
    @Autowired
    private IAgrBasePlotsService iAgrBasePlotsService;
    @Autowired
    private AgrBaseProcessService agrBaseProcessService;

    @Autowired
    private IAgrBaseOperateService iAgrBaseOperateService;

    /**
     * 查询批次信息
     * @param agrBatchBreedVo
     * @return
     */
    @Override
    public List<AgrBatchBreedVo> selectAgrBatchBreedList(AgrBatchBreedVo agrBatchBreedVo) {
        return agrBaseBatchMapper.selectAgrBatchBreedList(agrBatchBreedVo);
    }

    /**
     * 新增批次信息
     * @param agrBaseBatch
     * @return 成熟时间根据品种表获取数据自动计算
     * @author zjk
     */
    @Override
    public int insertAgrBaseBatch(AgrBaseBatch agrBaseBatch) {
        // 计算 成熟时间
        agrBaseBatch.setEndTime(String.valueOf(HarvestDateCalculator.calculateHarvestDates(agrGermBreedService.selectAgrGermBreedByBreedId(agrBaseBatch.getBreedId()).getBreedPeriod(), agrBaseBatch.getStartTime())[2]));

        return agrBaseBatchMapper.insert(agrBaseBatch);
    }

    /**
     * 修改批次信息
     * @param agrBaseBatch
     * @return
     */
    @Override
    @Transactional
    public int updateAgrBaseBatch(AgrBatchBreedVo agrBaseBatch) {
        // 计算 成熟时间
        agrBaseBatch.setEndTime(String.valueOf(HarvestDateCalculator.calculateHarvestDates(agrGermBreedService.selectAgrGermBreedByBreedId(agrBaseBatch.getBreedId()).getBreedPeriod(), agrBaseBatch.getStartTime())[2]));

        int j=0;
        AgrBasePlots agrBasePlots=new AgrBasePlots();
        //分配地块后修改批次状态为 0 已分配
        if (agrBaseBatch.getStatus()==1 && agrBaseBatch.getPlotsId()!=null) { //判断更改的字段是否包含地块
            agrBaseBatch.setStatus(0);
            //同时修改地块状态为 1 已种植
            agrBasePlots.setPlotsStatus(1l);
            agrBasePlots.setPlotsId(agrBaseBatch.getPlotsId());
            j= iAgrBasePlotsService.updateAgrBasePlots(agrBasePlots);

            // 分配地块时往种植操作表插数据
            //先查询process表
            List<AgrBaseProcess> list= agrBaseProcessService.getProcessByTypeId(agrBaseBatch.getTypeId());
            if (list != null) {
                for (AgrBaseProcess agrBaseProcess:list) {
                    AgrBaseOperate agrBaseOperate=new AgrBaseOperate();
                    agrBaseOperate.setBatchId(agrBaseBatch.getBatchId());
                    agrBaseOperate.setProcessSort(agrBaseProcess.getProcessId());
                    iAgrBaseOperateService.insertAgrBaseOperate(agrBaseOperate);
                }
            }else  throw new IllegalArgumentException("未查询到相关种植流程");

        }
        //如果 2 已收获     则释放地块为 0 空闲中
        if (agrBaseBatch.getStatus()==2) {
            agrBasePlots.setPlotsStatus(0l);
            agrBasePlots.setPlotsId(agrBaseBatch.getPlotsId());
            j= iAgrBasePlotsService.updateAgrBasePlots(agrBasePlots);
        }

        int i= agrBaseBatchMapper.updateById(agrBaseBatch);
        return i+j;
    }

    /**
     * 删除批次信息
     * @param batchId
     * @return
     */
    @Override
    public int deleteAgrBaseBatch(Long batchId) {

        return agrBaseBatchMapper.deleteById(batchId);
    }

    @Override
    public Long countBatch(Integer status) {
       QueryWrapper<AgrBaseBatch> wrapper = new QueryWrapper<>();
    if (status != null) {
        wrapper.eq("status", status);
    }
return agrBaseBatchMapper.selectCount(wrapper);

    }

    /**
     * @author gmy
     * 获取成熟批次
     * @return
     */
    @Override
    public List<Map<String, Object>> getBatchMature() {
        return agrBaseBatchMapper.selectByStatus(2);
    }
}
